#include "http_packet_parsing.h"

int check_http_packet_rece_finish(uint8_t *packet,uint32_t len)
{
	uint8_t *i = 0;
	uint8_t *j = 0;
	uint32_t data_len = 0;

	while (1)
	{
		i = str_str(packet,HTTP_HEAD);
		if (i)
		{
			if (str_str(packet, HTTP_DATA) != 0)
			{
				j = str_str(packet,HTTP_CONTENT_LENGTH);
				if(j)
				{
					data_len = Atoi(j + 16);
				}
				else
				{
					return FALSE;
				}

				j = str_str(packet, HTTP_DATA);
				if(*(j+data_len+3) != 0)
				{
					return TRUE;
				}
			}
			else
			{
				return FALSE;
			}
		}
		else
		{
			return FALSE;
		}
	}
}

int get_http_packet_content(uint8_t *packet, uint8_t *keyword, uint8_t *buf)
{
	uint8_t *i = 0;
	uint8_t *j = 0;
	uint32_t cnt = 0;
	uint32_t data_len;

	if ((i = str_str(packet, keyword)))
	{
		if (str_str(keyword, HTTP_DATA))
		{
			j = str_str(packet, HTTP_CONTENT_LENGTH);
			data_len = Atoi(j+16);

			while (data_len--)
			{
				*buf = *(char *)(i + 4);
				buf++;
				i++;
			}
			return data_len;
		}

		while (1)
		{
			if(*(i+str_len(keyword)-1+cnt) != '\r')
			{
				buf[cnt] = *(i + str_len(keyword) + cnt);
				cnt++;
			}
			else
			{
				break;
			}
		}
	}
	else
	{
		return FALSE;
	}

	return cnt;
}

